/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import org.antlr.runtime.RecognitionException;
import org.junit.Test;

import ac.jp.u_tokyo.SyncLib.Mod;
import ac.jp.u_tokyo.SyncLib.NullMod;
import ac.jp.u_tokyo.SyncLib.PrimMod;
import ac.jp.u_tokyo.SyncLib.dictionaries.DictModFactory;
import ac.jp.u_tokyo.SyncLib.modParser.ModParser;

public class DictModTest
{

	@Test
	public void testEqual() {
		DictModFactory<Integer, String> f1 = new DictModFactory<Integer, String>();
		f1.addPut(1, new PrimMod("a"));
		f1.addRemove(2);
		f1.addPut(3, new PrimMod("c"));
		DictModFactory<Integer, String> f2 = new DictModFactory<Integer, String>();
		f2.addRemove(2);
		f2.addPut(3, new PrimMod("c"));
		f2.addPut(1, new PrimMod("a"));
		assertEquals(f1.create(), f2.create());
		
	}
	
	@Test
	public void testNoConflict() {
		DictModFactory<Integer, String> f1 = new DictModFactory<Integer, String>();
		f1.addPut(1, new PrimMod("a"));
		f1.addRemove(2);
		f1.addPut(3, new PrimMod("c"));
		DictModFactory<Integer, String> f2 = new DictModFactory<Integer, String>();
		f2.addRemove(2);
		f2.addPut(3, new PrimMod("c"));
		f2.addPut(1, new PrimMod("a"));
		assertFalse(f1.create().isConflict(f2.create()));
		
	}
	
	@Test
	public void testSubtractEqual() throws RecognitionException {
		Mod mod1 = ModParser.parse("{a->1}")[0];
		Mod mod2 = ModParser.parse("{a->1}")[0];
		assertEquals(NullMod.INSTANCE, mod1.subtract(mod2));
	}

	@Test
	public void testSubtractNotEqual() throws RecognitionException {
		Mod mod1 = ModParser.parse("{a->1, b->2}")[0];
		Mod mod2 = ModParser.parse("{a->1}")[0];
		assertEquals(ModParser.parse("{b->2}")[0], mod1.subtract(mod2));
	}

	@Test
	public void testSubtractNull() throws RecognitionException {
		Mod mod1 = ModParser.parse("{b->2}")[0];
		Mod mod2 = ModParser.parse("nomod")[0];
		assertEquals(ModParser.parse("{b->2}")[0], mod1.subtract(mod2));
	}

}
